/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 *
 * @author spthanh
 */
public class Batches_Stmt_Example {
    public static void main(String[] args){
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks", "sa", "123");

            //Set fail to auto-commit
            con.setAutoCommit(false);

            //Create statement
            Statement stmt = con.createStatement();

            //Add SQL Commands to the batch
            stmt.addBatch("Insert INTO dbo.Student(FirstName,LastName,Address,Age,Description) VALUES('James1','Johnson1','23 Hai Ba trung',24,'Good student')");
            stmt.addBatch("Insert INTO dbo.Student(FirstName,LastName,Address,Age,Description) VALUES('James2','Johnson2','24 Hai Ba trung',25,'Good student')");
            stmt.addBatch("Insert INTO dbo.Student(FirstName,LastName,Address,Age,Description) VALUES('James3','Johnson3','25 Hai Ba trung',26,'Good student')");
            stmt.addBatch("Insert INTO dbo.Student(FirstName,LastName,Address,Age,Description) VALUES('James4','Johnson4','26 Hai Ba trung',27,'Good student')");

            //Execute the batch command
            int[] returnValueArr =  stmt.executeBatch();
            
            for(int item : returnValueArr){
                System.out.println("Value reflect:" + item);
            }
            

            //Commit the change into the database
            con.commit();

            //Enable auto-comit mode
            con.setAutoCommit(true);
            
            //Remove command in the batch
            stmt.clearBatch();

        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
